import { ResPage, ResultData } from "@/api/interface/index";
import { Ticket } from "@/api/interface/ticket";
import moment from "moment";
import { BASE } from "@/api/config/servicePort";
import http from "@/api";

/**
 * @name 工单管理模块
 */
// 获取待办工单列表
export const getPendingTicketListApi = (params: { page: number; page_size: number }) => {
  if (params["create_at"] && params["create_at"].length === 2) {
    params["start_time"] = moment(params["create_at"][0]).format("YYYY-MM-DD HH:mm:ss");
    params["end_time"] = moment(params["create_at"][1]).format("YYYY-MM-DD HH:mm:ss");
    delete params["create_at"];
  }
  return http.get<ResPage<Ticket.ResTicketList>>(`/ticket/api/ticket/pending/`, params);
};
// 获取已办工单列表
export const getFinishedTicketListApi = (params: { page: number; page_size: number }) => {
  if (params["create_at"] && params["create_at"].length === 2) {
    params["start_time"] = moment(params["create_at"][0]).format("YYYY-MM-DD HH:mm:ss");
    params["end_time"] = moment(params["create_at"][1]).format("YYYY-MM-DD HH:mm:ss");
    delete params["create_at"];
  }
  return http.get<ResPage<Ticket.ResTicketList>>(`/ticket/api/ticket/finished/`, params);
};
// 获取全部工单列表
export const getTicketListApi = (params: { page: number; page_size: number }) => {
  if (params["create_at"] && params["create_at"].length === 2) {
    params["start_time"] = moment(params["create_at"][0]).format("YYYY-MM-DD HH:mm:ss");
    params["end_time"] = moment(params["create_at"][1]).format("YYYY-MM-DD HH:mm:ss");
    delete params["create_at"];
  }
  return http.get<ResPage<Ticket.ResTicketList>>(`/ticket/api/ticket/`, params);
};
// 获取业务列表
export const getBusinessListApi = (params = { page: 1, page_size: 100 }) => {
  return http.get<ResPage<any>>(`/ticket/api/business/`, params);
};
// 获取业务详情
export const getBusinessDetailApi = (params: any) => {
  return http.get<ResPage<any>>(`/ticket/api/business/${params}/`);
};
// 获取工单详情
export const getTicketDetailApi = (params: any) => {
  return http.get<ResPage<any>>(`/ticket/api/ticket/${params}/`);
};
// 创建工单
export const createTicketApi = data => {
  return http.post<ResultData>(`/ticket/api/ticket/`, data);
};
// 批量删除工单
export const deleteTicketApi = params => {
  return http.delete<ResultData>(`/ticket/api/ticket/${params}/`);
};
// 批量删除工单
export const batchDeleteTicketApi = params => {
  return http.delete<ResultData>(`/ticket/api/ticket/multi_delete/`, params);
};
// 撤回工单
export const revokeTicketApi = params => {
  return http.post<ResultData>(`/ticket/api/ticket/${params.id ? params.id : params}/revoke/`);
};
// 退回工单
export const rejectTicketApi = params => {
  return http.post<ResultData>(`/ticket/api/ticket/${params}/reject/`);
};
// 获取资产列表
export const getAssetListApi = (params: { page: number; page_size: number }) => {
  return http.get<ResPage<any>>(BASE + `/asset/`, params);
};
// 获取资产列表的字段
export const getAssetFiledApi = () => {
  return http.get<ResultData>(BASE + `/field/asset/attrs/`);
};
// 获取告警列表
export const getAlarmListApi = (params: { page: number; page_size: number }) => {
  return http.get<ResPage<any>>(`alarm/api/alarms/`, params);
};
// 获取流程列表
export const getWorkflowListApi = (params = { page: 1, page_size: 100 }) => {
  return http.get<ResPage<any>>(`/ticket/api/workflow/`, params);
};
// 获取流程详情
export const getWorkflowDetailApi = params => {
  if (params) {
    return http.get<ResPage<any>>(`/ticket/api/workflow/${params}/`);
  } else {
    return http.get<ResPage<any>>(`/ticket/api/workflow/generic_flow/`);
  }
};
// 工单待办统计
export const getTicketDaily = () => {
  return http.get<any>(`/ticket/api/ticket/daily/`);
};
